//
//  ContentVideo.swift
//  BSBDJ
//
//  Created by apple on 2017/11/10.
//  Copyright © 2017年 incich. All rights reserved.
//

import UIKit
import BMPlayer

class ContentVideo: UIView {

    @IBOutlet weak var bgView: UIImageView!

    @IBOutlet weak var pictureView: UIImageView!

    @IBOutlet weak var playCountLabel: UILabel!

    @IBOutlet weak var videoTimeLabel: UILabel!

    @IBOutlet weak var palyButton: UIButton!
    
    var videoURL : String!
    var titleText : String!
    
    // 播放器
   lazy var player : BMPlayer = {
        let player = BMPlayer()
        addSubview(player)
        return player
      }()
  
    override func awakeFromNib() {
        super.awakeFromNib()
        playCountLabel.layer.cornerRadius = 3;
        videoTimeLabel.layer.cornerRadius = 3;
    }

    static public func contentVideo() -> ContentVideo {
        return Bundle.main.loadNibNamed("ContentVideo", owner: nil, options: nil)?.last as! ContentVideo

    }

    public func setupView(withContent content: ESContentModel) {
        
        
        let url = URL(string: content.large_image)

        pictureView.kf.setImage(with: url!)

        playCountLabel.text = "\(content.playcount)播放"

        let min = content.videotime / 60

        let second = content.videotime % 60

        videoTimeLabel.text = String(format: "%02d:%02d", min, second)
        
        // 如果是播放状态 暂停
        if player.isPlaying {
            player.pause()
        }
        
        // 播放页面
//        HLBMPlayerManager.shareSingleOne.setupFrame(frame: pictureView.frame)
//        HLBMPlayerManager.shareSingleOne.setupPlayerShowOrHidden(isShow: false)
        
        
       player.frame = pictureView.frame;
       player.isHidden = true

        // 播放按钮
        palyButton.addTarget(self, action: #selector(clickPublishButton), for: .touchUpInside)
        videoURL = content.videouri
        titleText = content.text
        

    }
    
    // 按钮点击事件 必须 加上@objc
    @objc fileprivate func clickPublishButton() {
        
        bgView.isHidden = true
        player.isHidden = false
        
//        player.updateUI(true)
        
        let ref = BMPlayerResource(url: URL(string:videoURL)!,
                                   name: titleText)
        player.setVideo(resource: ref)
    
        
        
//        HLBMPlayerManager.shareSingleOne.setupPlayerShowOrHidden(isShow: true)
//        HLBMPlayerManager.shareSingleOne.setupPlayerVideoInfor(videoUrl: videoURL, videoName: titleText)
      

    }

}
